package com.zzty.springmybatis.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzty.springmybatis.entity.SelfExam;
import com.zzty.springmybatis.entity.TeamExam;
import com.zzty.springmybatis.entity.User;
import com.zzty.springmybatis.service.SelfExamService;
import com.zzty.springmybatis.service.SelfExamTransationService;
import com.zzty.springmybatis.service.TeamExamService;
import com.zzty.springmybatis.service.TeamExamTransationService;
import com.zzty.springmybatis.service.UserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/SearchScore")
public class SearchScoreController {

	@Autowired
	SelfExamService ses;

	@Autowired
	SelfExamTransationService sets;

	@Autowired
	UserService us;

	@Autowired
	TeamExamService tes;

	@Autowired
	TeamExamTransationService tets;

	@RequestMapping(value = "/search/{page}", produces = { "text/html;charset=UTF-8;", "application/json;" })
	@ResponseBody
	public String judge(HttpSession httpSession, @RequestBody @PathVariable(value = "page") Integer page1) {

		 User user = (User) httpSession.getAttribute("User");
//		User user = new User();
//		user.setClazzId(4);
		int classid = Integer.valueOf(user.getClazzId());
		// System.out.println("allDate:" + allDate);
		Integer page = page1 * 10;
		List<SelfExam> tenSelfExam = ses.searchTenSelfExamByClazzId(classid, page);

		String dates[] = new String[tenSelfExam.size()];
		for (int i = 0; i < dates.length; i++) {
			dates[i] = tenSelfExam.get(i).getDate();

		}
		JSONArray jsonArray = new JSONArray();
		JSONArray jsonArray1 = new JSONArray();
		JSONArray jsonArray2 = new JSONArray();
		System.out.println("-------" + dates.length);
		Integer selfExamId = 0;
		for (int i = 0; i < dates.length; i++) {
			String date = dates[i];
			Integer examId = ses.searchIdByDateAndClass(date, classid);
			selfExamId = examId;
			if (examId != null) {
				JSONObject json = new JSONObject();
				Map<String, String> stuIdandScoreSelf = sets.searchIdAndScore(examId);

				String selfnames = "";
				String selfscores = "";
				for (Object obj : stuIdandScoreSelf.keySet()) {
					String key = (String) obj;
					String name = "";
					try {
						User u = new User();
						u.setId(Integer.valueOf(key));

						User stu = us.selectUserInfo(u);
						// User stu = ud.findAllByID(Integer.valueOf(key));
						if (stu.getType() != null) {
							continue;
						}
						name = stu.getDisname();
					} catch (Exception e) {
						e.printStackTrace();
					}
					String value = (String) stuIdandScoreSelf.get(key);
					if (value == null) {
						value = " ";
					}
					System.out.print(key + "=" + value + "\t");
					selfnames += (name + "_");
					selfscores += (value + "_");
				}
				json.put("date", date);
				json.put("names", selfnames);
				json.put("scores", selfscores);
				jsonArray1.add(json);
			}
		}
		List<TeamExam> tenTeamExam = tes.searchTenTeamExamByClazzId(classid, page);
		String[] teamDate = new String[tenTeamExam.size()];
		for (int i = 0; i < teamDate.length; i++) {
			teamDate[i] = tenTeamExam.get(i).getDate();

		}

		for (int i = 0; i < teamDate.length; i++) {
			String date = teamDate[i];
			Integer examId = tes.searchExamId(date, classid);
			if (tes.searchExamId(date, classid) == null) {
				examId = 0;
			}
			if (examId != 0) {
				JSONObject json = new JSONObject();

				Map<String, String> stuIdandScoreTeam = tets.searchIdAndScore(examId);
				String teamnames = "";
				String teamscores = "";
				for (Object obj : stuIdandScoreTeam.keySet()) {
					String key = (String) obj;
					String value = (String) stuIdandScoreTeam.get(key);
					System.out.println("key:" + key + "   value:" + value);
					if (value == null) {
						value = " ";
					}
					String name = "";
					String score = " ";
					if (key.length() > 3) {
						String tmps[] = key.split("\\|");
						try {
							User u1 = new User();
							u1.setId(Integer.valueOf(tmps[0]));
							User stu1 = us.selectUserInfo(u1);
							u1.setId(Integer.valueOf(tmps[1]));
							User stu2 = us.selectUserInfo(u1);

							// User stu1 =
							// ud.findAllByID(Integer.valueOf(tmps[0]));
							// User stu2 =
							// ud.findAllByID(Integer.valueOf(tmps[1]));
							name = stu1.getDisname() + "_" + stu2.getDisname();
							score = value + "_" + value;
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else {
						try {
							User u1 = new User();
							u1.setId(Integer.valueOf(key));
							User stu = us.selectUserInfo(u1);
							// User stu = ud.findAllByID(Integer.valueOf(key));
							name = stu.getDisname();
							score = value;
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					// System.out.print(name + "=" + score + "\t");
					teamnames += (name + "_");
					teamscores += (score + "_");
				}

				System.out.println("teamnames:" + teamnames);
				System.out.println("teamscores:" + teamscores);
				Map<String, String> team = new HashMap<String, String>();
				String tmpname[] = teamnames.split("_");
				String tmpscore[] = teamscores.split("_");
				for (int j = 0; j < tmpname.length; j++) {
					System.out.println("name:" + tmpname[j]);
					System.out.println("score:" + tmpscore[j]);
					team.put(tmpname[j], tmpscore[j]);

				}
				String newTeamNames = "";
				String newTeamScore = "";
				List<Integer> stuId = us.searchStuIdByClazzID(classid);
				// Map<String, String> stuIdandScoreSelf =
				// sets.searchIdAndScore(selfExamId);
				for (Object obj : stuId) {
					System.out.println();

					String tempUserId = obj+"";
					User u1 = new User();
					u1.setId(Integer.valueOf(tempUserId));
					User stu = us.selectUserInfo(u1);
					String stuName = stu.getDisname();
					System.out.println("selfexam name:" + stuName);
					newTeamNames += (stuName + "_");
					newTeamScore += (team.get(stuName) + "_");

				}

				json.put("date", date);
				json.put("names", newTeamNames);
				json.put("scores", newTeamScore);
				jsonArray2.add(json);
			}
		}
		jsonArray.add(jsonArray1);
		jsonArray.add(jsonArray2);
		System.out.println(jsonArray1.toString());
		System.out.println(jsonArray2.toString());
		System.out.println(jsonArray.toString());

		return jsonArray.toString();
		// out.print(jsonArray.toString());
	}

	@RequestMapping(value = "/searchMax", produces = { "text/html;charset=UTF-8;", "application/json;" })
	@ResponseBody
	public String searchMaxPage(HttpSession httpSession) {
		 User user = (User) httpSession.getAttribute("User");
		int classid = Integer.valueOf(user.getClazzId());
		List<SelfExam> allSelfExam = ses.searchSelfExamByClazzId(classid);
		System.out.println("classid:"+classid+"size:"+allSelfExam);
		System.out.println("size:"+allSelfExam);
		List<TeamExam> allTeamExam = tes.searchTeamExamByClazzId(classid);
		Integer maxCount = allSelfExam.size() > allTeamExam.size() ? allSelfExam.size() : allTeamExam.size();
		Integer maxPage = maxCount/10;
		return maxPage + "";
	}

	@RequestMapping(value = "/toSearch")
	public String toStudentScore() {
		return "StudentScore";

	}

}
